﻿using SmartSpace.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartSpace.OA
{
    public partial class OAService : IOAService
    {
        public ServiceResult SaveDocItem(DocItem item)
        {
            using (OADbContext context = new OADbContext())
            {
                EntityRepository.SaveCodeEntity(item, context);
                return new ServiceResult(EntityRepository.Commit(context));
            }
        }

        public List<DocItem> GetDocItems(Guid parentId)
        {
            using (OADbContext context = new OADbContext())
            {
                if (parentId == Guid.Empty)
                {
                    return context.Set<DocItem>().Where(p => p.ParentId.Value == Guid.Empty || p.ParentId == null).ToList();
                }
                else
                {
                    return context.Set<DocItem>().Where(p => p.ParentId.Value == parentId).ToList();
                }
            }
        }

        public List<DocItem> GetDocItems(Guid parentId, string type)
        {
            using (OADbContext context = new OADbContext())
            {
                if (parentId == Guid.Empty)
                {
                    return context.Set<DocItem>().Where(p => (p.ParentId.Value == Guid.Empty || p.ParentId == null) && p.TypeCode == type).ToList();
                }
                else
                {
                    return context.Set<DocItem>().Where(p => p.ParentId.Value == parentId && p.TypeCode == type).ToList();
                }
            }
        }

        public DocItem GetDocItem(Guid id)
        {
            using (OADbContext context = new OADbContext())
            {
                return context.Set<DocItem>().Where(p => p.Id == id).SingleOrDefault();
            }
        }

        public ServiceResult DeleteDocItem(Guid id)
        {
            using (OADbContext context = new OADbContext())
            {
                DocItem item = context.Set<DocItem>().Where(p=>p.Id == id).SingleOrDefault();
                EntityRepository.DeleteCodeEntity(item, context);
                return new ServiceResult(EntityRepository.Commit(context));
            }
        }


        public ServiceResult SaveDocFile(DocFile file)
        {
            ServiceResult result = null;
            using (OADbContext context = new OADbContext())
            {
                if (EntityRepository.Save<DocFile>(file, context))
                {
                    if (EntityRepository.Commit(context))
                    {
                        result = new ServiceResult(true);
                    }
                    else
                    {
                        result = new ServiceResult(false);
                    }
                }
                else
                {
                    result = new ServiceResult(false);
                }
            }
            return result;
        }

        public DocFile GetDocFile(Guid id)
        {
            using (OADbContext context = new OADbContext())
            {
                return context.DocFileSet.Where(p => p.Id == id).SingleOrDefault();
            }
        }
    }
}
